package com.musiclibrary.scraper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.musiclibrary.common.JsoupUtils;
import com.musiclibrary.persistence.MusicDirectory;

public class MusicDirectoryParser {

	public MusicDirectory parseMusicDirectory(Element element,final WebPage<Map<String, String>> webPage) {
		MusicDirectory musicDirectory = null;
		try {
			if (element != null) {
				musicDirectory=new MusicDirectory();

				Element hrefElement = element.tagName("a");
				// retrieve dir Name from element
				String dirName = hrefElement.html();
				musicDirectory.setDirectoryName(dirName);
				
				// retrieve URL from element
				String url = hrefElement.attr("href");
				musicDirectory.setDirectoryUrl(url);

				//update webPage with request parameter
				webPage.setUrl(url);				
				//fetch site data 
				JsoupUtils jsoupUtils = new JsoupUtils();
				Document document = jsoupUtils.loadPage(webPage);
				
				//retrieve music category
				List<Element> musicCategoryElementList=new ArrayList<Element>();
				if(document!=null){
					//update web page URL
					webPage.setUrl("http://telugump3downloads.net/music/index.php");
					
					//read header
					Elements headerElements = document.select("body div.head2");
					Element header = headerElements.get(0);
					String title = header.val();
					//header end
					
					//read category element from current page
					Elements elementList = document.select("body div.bg");
					if(elementList!=null && elementList.size()>0){
						musicCategoryElementList.addAll(elementList);
					}
					//read category element end
					
					//read category element from other pages

					//read index count
					Element bodyElement = document.tagName("body");
					String html = bodyElement.html();
					String substring = html.substring(html.indexOf("</div>Page:"), html.indexOf("<br />"));
					String pageStart = substring.substring(substring.indexOf(":")+1, substring.indexOf("O")).trim();
					String pageEnd = substring.substring(substring.lastIndexOf("Of")+2,substring.length()).trim();
					//read index count end
					
					int startCount = Integer.parseInt(pageStart);
					int endCount = Integer.parseInt(pageEnd);
					for(int i=startCount;i<endCount;i++){
						
						HashMap<String, String> hashMap = new HashMap<String, String>();
						hashMap.put("dir", "/"+title);
						hashMap.put("p", "1");
						hashMap.put("page", Integer.toString(i));
						hashMap.put("sort", "0");
						webPage.getFormHandler().updateForm(hashMap);
						
						Document _document = jsoupUtils.loadPage(webPage);
						if(_document!=null){
							Elements _elementList = _document.select("body div.bg");
							if(_elementList!=null && _elementList.size()>0){
								musicCategoryElementList.addAll(_elementList);
							}														
						}						
					}
					//read other pages end
					
					//construct music category list from element
					MusicCategoryParser musicCategoryParser = new MusicCategoryParser();
					if(musicCategoryElementList.size()>0){
						for(Element categoryElement:musicCategoryElementList){
							
						}
					}
					//construct music category list end
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return musicDirectory;
	}
}
